package com.yq.config;


import com.yq.interceptor.MyInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@ComponentScan("com.yq.controller")
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {


    //TODO 文件上传
    @Bean
    public MultipartResolver multipartResolver(){
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setDefaultEncoding("utf-8");
        return  multipartResolver;
    }

    //TODO 静态资源加载的路径
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img1/**").addResourceLocations("file:d:/spring/");
        registry.addResourceHandler("/img2/**").addResourceLocations("/");
        registry.addResourceHandler("/img3/**").addResourceLocations("classpath:/");
    }


    //TODO 配置interceptors
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new MyInterceptor()).addPathPatterns("/hello");
//        registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**");
//        registry.addInterceptor(new MyInterceptor3()).addPathPatterns("/hello/*");
    }
}
